#include "deleteuser.h"
#include "ui_deleteuser.h"
#include "string"
#include "QMessageBox"
#include "../MainProject/sqlitedatabase.h"
deleteUser::deleteUser(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::deleteUser)
{
    ui->setupUi(this);
      window()->setWindowTitle("删除用户");
}

void deleteUser::setFirstWindow(AdminManager *firstWindow)
{
     this->firstwindow = firstWindow;
}

void deleteUser::setDeleteUserName(const QString &userName)
{
    ui->deleteEdit->setText(userName);
    // 将用户名编辑框设置为只读


}

deleteUser::~deleteUser()
{
    delete ui;
}

void deleteUser::on_saveButton_clicked()
{
    QString deleteUsername= ui->deleteEdit->text();
    //通过用户名来删除
    if(deleteUsername.isEmpty()){
        QMessageBox::information(this, "waring", "删除的用户名不能为空");

    }
    if(!deleteUsername.isEmpty()){
        SQLiteDatabase* db = SQLiteDatabase::getInstance();
        QString condition = QString("username = '%1'").arg(deleteUsername); // 设置条件字符串
        QJsonArray result = db->read("users", condition); // 调用read方法获取结果
        if(result.isEmpty()){
            //  qDebug() << "找不到你要修改的用户" ;
            QMessageBox::information(this, "waring", "暂无你要删除的用户，请重新输入");
        }
        else{
            db->deleteRecord("users", "username = '" + deleteUsername + "'");
             QMessageBox::information(this, "waring", "删除成功");

             this->hide();
             QJsonArray result = db->read("users", "");


             firstwindow->selectAllUsers(result);
            //firstwindow->selectAllUsers();
        }

    }
}

